package com.example.demo.controller;

import com.example.demo.entity.UserMsg;
import com.example.demo.mapper.UserMsgMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @program: demo
 * @description: 1225下午作业
 * @author: Zwill
 * @create: 2019-12-25 14:18
 **/

@Api(tags = "usermsg")
@RestController
@EnableSwagger2
public class UserMsgController {
    @Autowired
    private UserMsgMapper userMsgMapper;

    /*i.人员姓名不能重复（校验）添加用户*/
    @ApiOperation("用户添加")
    @PostMapping("/addusermsg")
    public int addUserMsg(UserMsg userMsg) {
        List<String> usernames = userMsgMapper.findAllUserNames();
        if (userMsg != null) {
            if (userMsg.getUname() != null && !usernames.contains(userMsg.getUname())) {
                return userMsgMapper.insertUserMsg(userMsg);
            }
            System.out.println("用户名为空或者已经存在！");
        }
        return 0;
    }

    public List<String> findAllUserNames() {
        return userMsgMapper.findAllUserNames();
    }

    /*ii.一个人员可以对多个角色，查询人员所对应的角色*/
    @ApiOperation("查询人员对应的角色")
    @PostMapping("/findUserRoles")
    public List<String> findUserRoles(String uname) {
        return userMsgMapper.findUserRoles(uname);
    }

    /*iii.查询条件：创建时间区间查询*/
    @ApiOperation("时间区间查询")
    @PostMapping("/queryByTimeZone")
    public List<UserMsg> queryByTimeZone(String starttime, String endtime) {
        return userMsgMapper.queryByTimeZone(starttime, endtime);
    }

    /*iii.查询条件：姓名模糊查询*/
    @ApiOperation("姓名模糊查询")
    @PostMapping("/userFuzzyQueryByName")
    public List<UserMsg> fuzzyQueryByName(String fuzzyUname) {
        return userMsgMapper.fuzzyQueryByName(fuzzyUname);
    }

    /*iv.显示列表：人员姓名、邮箱、角色名称、创建时间*/
    @ApiOperation("显示人员全部信息")
    @PostMapping("/showAllMsgs")
    public List<UserMsg> showAllMsgs() {
        return userMsgMapper.showAllMsgs();
    }

    /*根据id删除用户*/
    @ApiOperation("删除用户")
    @GetMapping("/deleteUserMsg")
    public int deleteUserMsg(int uid) {
        return userMsgMapper.deleteUserMsg(uid);
    }

    /*更新用户信息*/
    @ApiOperation("更新用户信息")
    @PostMapping("/updateUserMsg")
    public int updateUser(UserMsg userMsg) {
        return userMsgMapper.updateUser(userMsg);
    }
}
